extern crate clap;

extern crate rustphp_vm;

use std::path::Path;

use clap::{App, Arg};

use rustphp_vm::{util, VirtualMachine};

fn main() {
    let matches = App::new("RustPHP")
        .about("用Rust实现的PHP虚拟机")
        .arg(Arg::with_name("INPUT"))
        .get_matches();

    match matches.value_of("INPUT") {
        Some(file_name) => run_script(file_name),
        _ => println!("没有输入脚本"),
    }
}

/// 执行PHP脚本
fn run_script(file_name: &str) {
    let file_path = Path::new(file_name);

    match util::read_file(file_path) {
        Ok(source) => VirtualMachine::new().run_script(&source),
        Err(_err) => {
            std::process::exit(1);
        }
    }
}
